package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.SysParamBean;
import cc.rengu.igas.bomp.facade.request.OltpParamInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.OltpParamInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 公共参数查询服务
 * Created by 王成 on 2019/1/25.
 */
public class OltpParamInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        OltpParamInfoQueryRequest oltpParamInfoQueryRequest = new OltpParamInfoQueryRequest();
        ConvertUtil.convertOutput(oltpParamInfoQueryRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, oltpParamInfoQueryRequest.getHeader().getTraceNo());
        return oltpParamInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        OltpParamInfoQueryRequest oltpParamInfoQueryRequest = (OltpParamInfoQueryRequest) request;
        List<SysParamBean> sysParamBeanList = new ArrayList<>();
        SysParamService sysParamService = new SysParamServiceImpl();
        if (null != oltpParamInfoQueryRequest.getParamType() && !oltpParamInfoQueryRequest.getParamType().isEmpty()) {
            if (null != oltpParamInfoQueryRequest.getParamKey() && !oltpParamInfoQueryRequest.getParamKey().isEmpty()) {
                SysParam sysParam = sysParamService.getSysParamInfo(oltpParamInfoQueryRequest.getHeader().getInstId(), oltpParamInfoQueryRequest.getParamType(), oltpParamInfoQueryRequest.getParamKey());
                if (null != sysParam) {
                    convSysParamInfo(sysParam, sysParamBeanList);
                }
            } else {
                List<SysParam> sysParamList = sysParamService.getSysParamInfo(oltpParamInfoQueryRequest.getHeader().getInstId(), oltpParamInfoQueryRequest.getParamType());
                if (null != sysParamList && !sysParamList.isEmpty()) {
                    sysParamList.forEach(item -> convSysParamInfo(item, sysParamBeanList));
                }
            }
        } else {
            List<SysParam> sysParamList = sysParamService.getSysParamInfo(oltpParamInfoQueryRequest.getHeader().getInstId(), oltpParamInfoQueryRequest.getParamType());
            if (null != sysParamList && !sysParamList.isEmpty()) {
                sysParamList.forEach(item -> convSysParamInfo(item, sysParamBeanList));
            }
        }
        /* 拼装应答报文 */
        OltpParamInfoQueryResponse oltpParamInfoQueryResponse = new OltpParamInfoQueryResponse();
        oltpParamInfoQueryResponse.getHeader().setTxnNum(oltpParamInfoQueryRequest.getHeader().getTxnNum());
        oltpParamInfoQueryResponse.getHeader().setTraceNo(oltpParamInfoQueryRequest.getHeader().getTraceNo());
        oltpParamInfoQueryResponse.setParamInfo(sysParamBeanList);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(oltpParamInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        OltpParamInfoQueryResponse oltpParamInfoQueryResponse = (OltpParamInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(oltpParamInfoQueryResponse);
    }

    /**
     * 系统参数信息转换
     *
     * @param sysParam         系统参数表数据
     * @param sysParamBeanList 系统参数信息
     */
    private void convSysParamInfo(SysParam sysParam, List<SysParamBean> sysParamBeanList) {
        SysParamBean sysParamBean = new SysParamBean();
        BeanUtil.beanCopy(sysParam, sysParamBean);
        sysParamBeanList.add(sysParamBean);
    }
}
